package pulsar.schema.json;

import org.apache.pulsar.client.api.Producer;
import org.apache.pulsar.client.api.PulsarClient;
import org.apache.pulsar.client.api.PulsarClientException;
import org.apache.pulsar.client.api.Schema;
import pulsar.schema.bean.Person;
import pulsar.schema.bean.User;

/**
 * 
 * @version 1.0
 * @date 2021/3/24 17:26
 */
public class MyProducer {

    static PulsarClient client;

    static {
        try {
            client = PulsarClient.builder()
                        .serviceUrl("pulsar://192.168.75.237:6650")
                        .build();
        } catch (PulsarClientException e) {
            e.printStackTrace();
        }
    }

    static void sendMsg() throws PulsarClientException {
        //JSON SCHEMA
        Producer<Person> stringProducer = client.newProducer(Schema.JSON(Person.class))
                .topic("my-topic-json")
                .create();
        Person person = Person.builder()
                .id("KFC666")
                .user(User.builder()
                        .age(18)
                        .name("张三")
                        .nick("sdasadafa")
                        .build())
                .build();
        stringProducer.send(person);

        //close
//        producer.closeAsync()
        stringProducer.closeAsync()
                .thenRun(() -> System.out.println("stringProducer closed"));
    }

    public static void main(String[] args) throws PulsarClientException {
        sendMsg();
        client.close();
    }
}
